﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;

namespace blackhouse.Web.Controls {
	public class DynamicImage : System.Web.UI.WebControls.Image {

		#region Fields

		private string configuration;
		private string alterConfiguration;
		private string fileName;
		private DynamicImageType type;
		private int width;
		private int height;

		#endregion

		#region Properties

		public string Configuration {
			get { return this.configuration; }
			set {
				if (value != this.configuration) {
					this.configuration = value;
					this.SetUrl();
				}
			}
		}

		public string AlternateConfiguration {
			get { return this.alterConfiguration; }
			set {
				if (value != this.alterConfiguration) {
					this.alterConfiguration = value;
					this.SetUrl();
				}
			}
		}

		public override System.Web.UI.WebControls.Unit Width {
			get {
				return base.Width;
			}
			set {
				if (value != base.Width) {
					base.Width = value;
					this.SetUrl();
				}
			}
		}

		public override System.Web.UI.WebControls.Unit Height {
			get {
				return base.Height;
			}
			set {
				if (value != base.Height) {
					base.Height = value;
					this.SetUrl();
				}
			}
		}

		public string FileName {
			get { return this.fileName; }
			set {
				if (value != this.fileName) {
					this.fileName = value;
					this.SetUrl();
				}
			}
		}

		public DynamicImageType Type {
			get { return this.type; }
			set {
				if (value != this.type) {
					this.type = value;
					this.SetUrl();
				}
			}
		}

		public string Size {
			get { return width + "x" + height; }
			set {
				if (!string.IsNullOrEmpty(value)) {
					int w, h;
					string[] parts = value.Split('x');
					if (parts.Length == 2
						&& Int32.TryParse(parts[0], out w)
						&& Int32.TryParse(parts[1], out h)
						&& (w != this.width || h != this.height)
						) {
						this.width = w;
						this.height = h;
						this.SetUrl();
					}
				}
			}
		}

		private static bool useRewriteModule = false;
		private static bool useRewriteModule_Set = false;


		private const string URL = "/{0}.ashx?configuration={1}&width={2}&height={3}&file={4}";
		private const string URL_REWRITED = "/{0}/{1}/{2}x{3}/{4}";

		private string UrlBase {
			get {
				if (!useRewriteModule_Set) {
					useRewriteModule = HttpRuntime.UsingIntegratedPipeline;
					useRewriteModule_Set = true;
				}
				return (useRewriteModule) ? URL_REWRITED : URL;
			}
		}

		#endregion

		#region Methods

		private void SetUrl() {

			if (width == 0 && this.Width != null && this.Width.Type == System.Web.UI.WebControls.UnitType.Pixel)
				width = Convert.ToInt32(this.Width.Value);
			if (height == 0 && this.Height != null && this.Height.Type == System.Web.UI.WebControls.UnitType.Pixel)
				height = Convert.ToInt32(this.Height.Value);

			this.ImageUrl = string.Format(UrlBase, (useRewriteModule) ? DynamicImageTypeOpe.GetShortName(Type) : DynamicImageTypeOpe.GetFullName(Type), Configuration, width, height, FileName);
			if (!string.IsNullOrEmpty(AlternateConfiguration)) {
				string aUrl = string.Format(UrlBase, (useRewriteModule) ? DynamicImageTypeOpe.GetShortName(Type) : DynamicImageTypeOpe.GetFullName(Type), AlternateConfiguration, width, height, FileName);
				Attributes.Add("altersrc", aUrl);
			}
		}

		#endregion

	}
}